/**
 * 
 */
package com.jolestar.yayaweibotong;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;

import com.jolestar.yayaweibotong.service.OAuthService;
import com.jolestar.yayaweibotong.service.StatusService;
import com.jolestar.yayaweibotong.service.impl.DoubanOAuthService;
import com.jolestar.yayaweibotong.service.impl.DoubanStatusService;
import com.jolestar.yayaweibotong.service.impl.SinaOAuthService;
import com.jolestar.yayaweibotong.service.impl.SinaStatusService;
import com.jolestar.yayaweibotong.service.impl.TwitterOAuthService;
import com.jolestar.yayaweibotong.service.impl.TwitterStatusService;

/**
 * @author jolestar@gmail.com
 * 
 */
public enum ServiceType {
	yaya("yaya", "yaya", "yaya", null, null, null, null, -1, true), sina(
			"新浪微博", "t.sina.com.cn", "http://t.sina.com.cn/", "265054478",
			"26dd576476fa02d850380b4529a40d89", new SinaOAuthService(),
			new SinaStatusService(), 140, true), twitter("Twitter",
			"twitter.com", "http://twitter.com/", "M2Ek4NkfOtOzX1hCDuQ",
			"RIFCEZfY1uCwR7fqYgwDBVH5Rkfmy3RALEH51gCjWo",
			new TwitterOAuthService(), new TwitterStatusService(), 140, true), douban(
			"豆瓣", "douban.com", "http://www.douban.com/people/",
			"07045bebb87fabbd150d13c3297f0337", "debd62861b9b0410",
			new DoubanOAuthService(), new DoubanStatusService(), 128, false);
	private String title;
	private String host;
	private String userDomainPrefix;
	private String apiKey;
	private String apiSecret;
	private int statusLengthLimit;
	private OAuthService oauthService;
	private StatusService statusService;
	private boolean enable;

	ServiceType(String title, String host, String userDomainPrefix,
			String apiKey, String apiSecret, OAuthService oauthService,
			StatusService statusService, int statusLengthLimit, boolean enable) {
		this.title = title;
		this.host = host;
		this.userDomainPrefix = userDomainPrefix;
		this.oauthService = oauthService;
		this.statusService = statusService;
		this.statusLengthLimit = statusLengthLimit;
		this.apiKey = apiKey;
		this.apiSecret = apiSecret;
		this.enable =enable;
	}

	public String getHost() {
		return this.host;
	}
	
	public String getUrl(){
		return "http://"+this.host;
	}

	public String getTitle() {
		return this.title;
	}

	public static ServiceType fromHost(String host) {
		for (ServiceType type : ServiceType.values()) {
			if (type.host.equalsIgnoreCase(host)) {
				return type;
			}
		}
		return null;
	}

	public static ServiceType fromName(String name) {
		if (name == null) {
			return null;
		}
		try {
			return ServiceType.valueOf(name);
		} catch (Exception e) {
			return null;
		}
	}

	public StatusService getStatusService() {
		return this.statusService;
	}

	public OAuthService getOAuthService() {
		return this.oauthService;
	}

	public static ServiceType[] getExternalServices() {
		List<ServiceType> types = new ArrayList<ServiceType>();
		for(ServiceType serviceType:ServiceType.values()){
			if(serviceType != yaya&& serviceType.enable){
				types.add(serviceType);
			}
		}
		return types.toArray(new ServiceType[types.size()]);
	}

	public int getStatusLengthLimit() {
		return this.statusLengthLimit;
	}

	public String getApiKey() {
		return apiKey;
	}

	public String getApiSecret() {
		return apiSecret;
	}

	public String formatDomainUrl(String domain) {
		return this.userDomainPrefix + domain;
	}
	
	public boolean isEnable(){
		return this.enable;
	}

}
